package com.xzr.controller;

import com.xzr.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * @Author：jojo
 * @Date：2022/2/10 17:33
 * @Description :TODO
 */
@RequestMapping("/UserManager")
@Controller
public class test {
    @RequestMapping("/addUser")
    public ModelAndView add(@Valid @RequestBody User user, BindingResult BindingResult){
        ModelAndView modelAndView = new ModelAndView();
        if (BindingResult.hasErrors()){
            FieldError fieldError = BindingResult.getFieldError();
            modelAndView.addObject("msg",fieldError.getField()+""+BindingResult.getFieldError().getDefaultMessage());
        }
        modelAndView.setViewName("/WEB-INF/jsp/error.jsp");
        return modelAndView;
    }
}
